﻿


--职级异动一级审批
CREATE proc [dbo].[proc_Employee_CompanyMove_Audit]
(
	@Id int,
	@Audit bit,
	@Memo text,
	@UserName nvarchar (50),
	@AuditTime nvarchar(50)

)
as
	BEGIN TRAN
	
	Declare @ACompanyId int, @ADepartmentId int,@AZhiWu nvarchar(50),@AuditPerson nvarchar(50),@eid int,@uname nvarchar(50)
	Select @ACompanyId=ACompanyId,@ADepartmentId=ADepartmentId,@AZhiWu=AZhiWu,@AuditPerson=AuditPerson,@eid=eid From employee_CompanyMove Where id = @id
	Select @uname=username from employee where id=@eid
		--一级审批

--			begin
--				update employee_CompanyMove set Audit=@Audit,Memo=@Memo,AuditPerson=@UserName,AuditTime=@AuditTime where id=@id
--				update employee set CompanyId=@ACompanyId,DepartmentId=@ADepartmentId,ZhiWu=@AZhiWu where id=@eid
--				
--				update aspnet_users set companyid=@ACompanyId where username=@uname
--			end
		--判断帐号表与员工档案表中公司ID与部门ID是否相同  
	IF(@Audit=1)--审批通过
	BEGIN
		DECLARE @Ecid INT,@Edid INT,@Ucid INT,@Udid INT
		SELECT @Ecid=CompanyId,@Edid=DepartmentId FROM Employee WHERE id=@eid
		SELECT @Ucid=CompanyId,@Udid=DepartmentId FROM aspnet_Users WHERE UserName=@uname
			BEGIN 			
					update employee_CompanyMove set Audit=@Audit,Memo=@Memo,AuditPerson=@UserName,AuditTime=@AuditTime where id=@id
				--	IF(@Ecid=@Ucid AND @Edid=@Udid)			
				--update aspnet_users set companyid=@ACompanyId,DepartmentId =@ADepartmentId where username=@uname
						
				--update employee set CompanyId=@ACompanyId,DepartmentId=@ADepartmentId,ZhiWu=@AZhiWu where id=@eid						
				 
			END		
			
	END	
	ELSE
		BEGIN
				update employee_CompanyMove set Audit=@Audit,Memo=@Memo,AuditPerson=@UserName,AuditTime=@AuditTime where id=@id
		END
COMMIT TRAN